//package com.yunhe.device.repository.device.impl;
//
//import com.yunhe.device.domain.device.Device;
//import com.yunhe.device.repository.device.DeviceRepository;
//import org.neo4j.ogm.model.Result;
//import org.neo4j.ogm.session.Session;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Repository;
//
//import java.util.*;
//
///**
// * 设备数据仓库自定义实现
// * @author liuronglei
// */
//@Repository
//public class DeviceRepositoryImpl implements DeviceRepository {
//    @Autowired
//    private Session session;
////    @Autowired
////    private ResourceTreeRepository resourceTreeRepository;
//
//    private List<Device> getDeviceList(Result results) {
//        List<Device> list = new ArrayList<>();
//        for (Map<String, Object> result : results) {
//            Device device = new Device();
//            Map<String, Object> object = (Map<String, Object>)result.get("object");
//            for (Map.Entry<String, Object> entry : object.entrySet()) {
//                device.put(entry.getKey(), entry.getValue());
//            }
//            list.add(device);
//        }
//        return list;
//    }
//
//    private Device getDevice(Result result) {
//        List<Device> devices = getDeviceList(result);
//        if (devices != null && devices.size() > 0) {
//            return devices.get(0);
//        }
//        return null;
//    }
//
//    @Override
//    public void bindEnergyUnit(Long energyUnitId, Long deviceId) {
//        //resourceTreeRepository.bindResource(energyUnitId, "Device", deviceId);
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("energyUnitId", energyUnitId);
//        paramMap.put("deviceId", deviceId);
//        String cypher = "start e=node({energyUnitId}),d=node({deviceId}) create (e)-[:CONTAINS]->(d)";
//        session.query(cypher, paramMap);
//    }
//
//    @Override
//    public void unbindEnergyUnit(Long energyUnitId, Long deviceId) {
//        //resourceTreeRepository.unbindResource(energyUnitId, deviceId);
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("energyUnitId", energyUnitId);
//        paramMap.put("deviceId", deviceId);
//        String cypher = "start e=node({energyUnitId}),d=node({deviceId}) match(e)-[r:CONTAINS]->(d) delete r";
//        session.query(cypher, paramMap);
//    }
//
//    /**
//     * 设备是否有绑定能量单元
//     * @param deviceId 设备ID
//     */
//    @Override
//    public boolean hasBindingEnergyUnit(Long deviceId) {
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("deviceId", deviceId);
//        String cypher = "start m=node({deviceId}) match(n)-[r:CONTAINS]->(m) return n";
//        Result result = session.query(cypher, paramMap);
//        Iterator<Map<String, Object>> iterator = result.iterator();
//        if (iterator.hasNext()) {
//            return true;
//        } else {
//            return false;
//        }
//    }
//
//    /**
//     * 根据电站ID获得所有设备列表
//     * Station->EnergyUnit->Device + Station->Device
//     * @param stationId 能量单元ID
//     */
//    @Override
//    public List<Device> findByStationId(Long stationId) {
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("stationId", stationId);
//        String cypher = "start s=node({stationId}) match(s)-[:HAS_RESOURCE{type:'EnergyUnit'}]->(e:EnergyUnit)-[:CONTAINS]->(object)," +
//                "(dType:DeviceType) with [x in labels(object) where x in dType.name] as types,object where size(types)=1" +
//                " return object{id:id(object),type:types[0],.*}" +
//                " union " +
//                "start s=node({stationId}) match(s)-[:HAS_RESOURCE{type:'Device'}]->(object)," +
//                "(dType:DeviceType) with [x in labels(object) where x in dType.name] as types,object where size(types)=1" +
//                " return object{id:id(object),type:types[0],.*}";
//        Result result = session.query(cypher, paramMap);
//        return getDeviceList(result);
//    }
//
//    /**
//     * 根据电站ID和设备类型获得所有设备列表
//     * @param stationId 能量单元ID
//     * @param type 设备类型
//     */
//    @Override
//    public List<Device> findByStationIdAndType(Long stationId, String type) {
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("stationId", stationId);
//        paramMap.put("type", type);
//        String cypher = "start s=node({stationId}) match(s)-[:HAS_RESOURCE{type:'EnergyUnit'}]->(e:EnergyUnit)-[:CONTAINS]->(object:" + type + ")" +
//                " return object{id:id(object),type:'{type}',.*}" +
//                " union " +
//                "start s=node({stationId}) match(s)-[:HAS_RESOURCE{type:'Device'}]->(object:" + type + ")" +
//                " return object{id:id(object),type:'{type}',.*}";
//        Result result = session.query(cypher, paramMap);
//        return getDeviceList(result);
//    }
//
//    /**
//     * 根据能量单元ID获得所有设备列表
//     * @param energyUnitId 能量单元ID
//     */
//    @Override
//    public List<Device> findByEnergyUnitId(Long energyUnitId) {
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("energyUnitId", energyUnitId);
//        String cypher = "start e=node({energyUnitId}) match(e)-[:CONTAINS]->(object)," +
//                "(dType:DeviceType) with [x in labels(object) where x in dType.name] as types,object where size(types)=1" +
//                " return object{id:id(object),type:types[0],.*}";
//        Result result = session.query(cypher, paramMap);
//        return getDeviceList(result);
//    }
//
//    /**
//     * 根据单位ID获得设备对象列表
//     * @param firmId 单位ID
//     */
//    @Override
//    public List<Device> findByFirmId(Long firmId) {
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("firmId", firmId);
//        String cypher = "start f=node({firmId}) match(f)-[:HAS_STATION]->(s:Station)-[:HAS_RESOURCE{type:'EnergyUnit'}]->(e:EnergyUnit)-[:CONTAINS]->(object)," +
//                "(dType:DeviceType) with [x in labels(object) where x in dType.name] as types,object where size(types)=1" +
//                " return object{id:id(object),type:types[0],.*}" +
//                " union " +
//                "start f=node({firmId}) match(f)-[:HAS_STATION]->(s:Station)-[:HAS_RESOURCE{type:'Device'}]->(object)," +
//                "(dType:DeviceType) with [x in labels(object) where x in dType.name] as types,object where size(types)=1" +
//                " return object{id:id(object),type:types[0],.*}";
//        Result result = session.query(cypher, paramMap);
//        return getDeviceList(result);
//    }
//
//    /**
//     * 根据能量单元ID和设备类型获得所有设备列表
//     * @param energyUnitId 能量单元ID
//     * @param type 设备类型
//     */
//    @Override
//    public List<Device> findByEnergyUnitIdAndType(Long energyUnitId, String type) {
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("energyUnitId", energyUnitId);
//        paramMap.put("type", type);
//        String cypher = "start n=node({energyUnitId}) match(n)-[:CONTAINS}]->(object:" + type + ")" +
//                " return object{id:id(object),type:'{type}',.*}";
//        Result result = session.query(cypher, paramMap);
//        return getDeviceList(result);
//    }
//
//    /**
//     * 根据设备ID获得其连接关系的指定设备类型的设备列表
//     * @param deviceId 设备ID
//     * @param type 设备类型
//     */
//    @Override
//    public List<Device> findByConnectRelation(Long deviceId, String type) {
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("deviceId", deviceId);
//        paramMap.put("type", type);
//        String cypher = "start n=node({deviceId}) match(n)<-[:HAS_SHUNT_DEV]-(:ConnectivityNode)-[:CONNECT_TO]" +
//                "-(:ConnectivityNode)-[:HAS_SHUNT_DEV]->(object:" + type + ") return object{id:id(object),type:'{type}',.*}";
//        Result result = session.query(cypher, paramMap);
//        return getDeviceList(result);
//    }
//
//    /**
//     * 根据设备ID获得设备对象
//     * @param deviceId 设备ID
//     */
//    @Override
//    public Device findById(Long deviceId) {
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("deviceId", deviceId);
//        String cypher = "start object=node({deviceId}) match(object),(dType:DeviceType)" +
//                " with [x in labels(object) where x in dType.name] as types,object where size(types)=1" +
//                " return object{id:id(object),type:types[0],.*}";
//        Result result = session.query(cypher, paramMap);
//        return getDevice(result);
//    }
//
//    /**
//     * 根据设备代码获得设备对象
//     * @param deviceCode 设备代码
//     */
//    @Override
//    public Device findByCode(Long deviceCode) {
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("deviceCode", deviceCode);
//        String cypher = "match(object),(dType:DeviceType) where object.code={deviceCode}" +
//                " with [x in labels(object) where x in dType.name] as types,object where size(types)=1" +
//                " return object{id:id(object),type:types[0],.*}";
//        Result result = session.query(cypher, paramMap);
//        return getDevice(result);
//    }
//
//    /**
//     * 新增设备
//     * @param energyUnitId 能量单元ID
//     * @param device 设备对象
//     */
//    @Override
//    public Device create(Long energyUnitId, Device device) {
//        String type = device.getType();
//        StringBuffer cypher = new StringBuffer("create (object:").append(type).append("{");
//        for (String key : device.keySet()) {
//            if (!"id".equals(key)) {
//                cypher.append(key).append(":{").append(key).append("},");
//            }
//        }
//        cypher.deleteCharAt(cypher.length() - 1);
//        cypher.append("}) return object{id:id(object),type:'{type}',.*}");
//        Result result = session.query(cypher.toString(), device);
//        Device newDevice = getDevice(result);
//        bindEnergyUnit(energyUnitId, newDevice.getId());
//        return newDevice;
//    }
//
//    /**
//     * 更新设备
//     * @param device 设备对象
//     */
//    @Override
//    public Device update(Device device) {
//        StringBuffer cypher = new StringBuffer("match(object) where ID(object)={id} set ");
//        for (String key : device.keySet()) {
//            cypher.append(key).append("={").append(key).append("},");
//        }
//        cypher.deleteCharAt(cypher.length() - 1);
//        cypher.append("}) return object{id:id(object),type:'{type}',.*}");
//        Result result = session.query(cypher.toString(), device);
//        return getDevice(result);
//    }
//
//    /**
//     * 删除设备
//     * @param deviceId 设备ID
//     */
//    @Override
//    public void deleteById(Long deviceId) {
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("deviceId", deviceId);
//        String cypher = "match(object) where ID(object)={deviceId} delete object";
//        session.query(cypher, paramMap);
//    }
//
//    /**
//     * 是否存在设备
//     * @param deviceId 设备ID
//     */
//    @Override
//    public boolean existsById(Long deviceId) {
//        Device device = findById(deviceId);
//        return device != null;
//    }
//}
